package com.jialianiot.myapplication;

import android.animation.AnimatorSet;
import android.animation.ValueAnimator;
import android.annotation.SuppressLint;
import android.app.Activity;
import android.graphics.BitmapFactory;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.support.v4.view.PagerAdapter;
import android.support.v4.view.ViewPager;
import android.support.v7.app.AppCompatActivity;
import android.view.Display;
import android.view.MotionEvent;
import android.view.View;
import android.view.ViewGroup;
import android.view.animation.Animation;
import android.view.animation.AnimationUtils;
import android.widget.ImageView;
import android.widget.LinearLayout;

import java.util.ArrayList;

public class MainActivity2 extends AppCompatActivity {


    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);


        /**播放九球动画*/
//        AnimatorHealthy90V1Util.doAnimatorPlay(this);


        initCenter(this);

        final ImageView image_bg_1 = findViewById(R.id.image_bg_1);
        final ImageView image_bg_2 = findViewById(R.id.image_bg_2);





        BitmapFactory.Options options = new BitmapFactory.Options();
        BitmapFactory.decodeResource(getResources(),R.mipmap.bg1,options);
        //获取图片的宽高
        float backgroundWidth = options.outWidth;
        float backgroundHeight = options.outHeight;
        Tools.logByWh("图片 - width = " + backgroundWidth + " height = " + backgroundHeight);

        //获取图片的真实宽高
        float backgroundHeight1= screenHeight;
        float backgroundWidth1 = screenHeight/backgroundHeight*backgroundWidth;
        Tools.logByWh("图片的真实 - width = " + backgroundWidth1 + " height = " + backgroundHeight1);


        ViewGroup.LayoutParams params = image_bg_1.getLayoutParams();
        params.height = screenHeight;

        float bl = backgroundWidth/ backgroundHeight;
        float realWidth1 = screenHeight*bl;
        Tools.logByWh("图片 - bl = " + bl + " realWidth1 = " + realWidth1);


        float realWidth = screenHeight/backgroundHeight*backgroundWidth;
        Tools.logByWh("图片 - screenHeight = " + screenHeight + " backgroundHeight = " + backgroundHeight + " backgroundWidth = " + backgroundWidth);
        Tools.logByWh("图片 - realWidth = " + realWidth);
        params.width = (int) realWidth;
        Tools.logByWh("图片 - width = " + screenHeight/backgroundHeight*backgroundWidth + " height = " + screenHeight);
        image_bg_1.setLayoutParams(params);
        image_bg_2.setLayoutParams(params);






        // 移动速度
        int speed = 1;
        // 图片宽度 + 屏幕宽度
//        float allWidth = image_bg_1.getLayoutParams().width + screenWidth;
        // 图片宽度 - 屏幕宽度
        float leftover = backgroundWidth1 - screenWidth;
        // 图片宽度 - 屏幕宽度  的 移动时长
        long leftoverTime = (long) (leftover * speed);
        // 屏幕宽度  的 移动时长
        long screenTime = (long) (screenWidth * speed);
        // 图片宽度  的 移动时长
        long backgroundTime = (long) (backgroundWidth1 * speed);

//        Tools.logByWh("allWidth=" + allWidth);
        Tools.logByWh("leftover=" + leftover);
        Tools.logByWh("leftoverTime=" + leftoverTime);
        Tools.logByWh("screenTime=" + screenTime);
        Tools.logByWh("backgroundTime=" + backgroundTime);






//        // 第一个动画，bg1移动图片宽度 减去 屏幕宽度
//        AnimatorSet ballAnimator1 = new AnimatorSet();
//        ValueAnimator ballAnimator1X = ValueAnimator.ofFloat(0, -leftover);
//        ballAnimator1X.addUpdateListener(new ValueAnimator.AnimatorUpdateListener() {
//            @Override
//            public void onAnimationUpdate(ValueAnimator animation) {
//                Tools.logByWh("" + (float) animation.getAnimatedValue());
//                image_bg_1.setX((float) animation.getAnimatedValue());
//                image_bg_1.requestLayout();
//            }
//        });
//        ballAnimator1X.setDuration(leftoverTime);
//        //组合播放
//        ballAnimator1.play(ballAnimator1X);
//        ballAnimator1.setStartDelay(0);
//        ballAnimator1.start();

        // 第二个动画，bg1移动屏幕宽度的同时（移动时，bg1就移出屏幕了），
        //      bg2移动图片宽度 x从屏幕宽度开始
        AnimatorSet ballAnimator2 = new AnimatorSet();
        Tools.logByWh("yyy|-leftover" + -leftover);
        Tools.logByWh("yyy|-backgroundWidth" + backgroundWidth1);
        Tools.logByWh("yyy|-screenWidth" + screenWidth);
        ValueAnimator ballAnimator2X = ValueAnimator.ofFloat(-leftover, -backgroundWidth1);
        ballAnimator2X.addUpdateListener(new ValueAnimator.AnimatorUpdateListener() {
            @Override
            public void onAnimationUpdate(ValueAnimator animation) {
                Tools.logByWh("yyy-" + (float) animation.getAnimatedValue());
                image_bg_1.setX((float) animation.getAnimatedValue());
                image_bg_1.requestLayout();
            }
        });
        ballAnimator2X.setDuration(screenTime);
        //      bg2移动图片宽度 x从屏幕宽度开始
        ValueAnimator ballAnimator3X = ValueAnimator.ofFloat(screenWidth, -leftover);
        ballAnimator3X.addUpdateListener(new ValueAnimator.AnimatorUpdateListener() {
            @Override
            public void onAnimationUpdate(ValueAnimator animation) {
                Tools.logByWh("xxx-" + (float) animation.getAnimatedValue());
                image_bg_2.setX((float) animation.getAnimatedValue());
                image_bg_2.requestLayout();
            }
        });
        ballAnimator3X.setDuration(backgroundTime);
//        ballAnimator3X.setDuration(leftoverTime);
        //组合播放
        ballAnimator2.play(ballAnimator2X).with(ballAnimator3X);//.after(leftoverTime)
//        ballAnimator2.setStartDelay(leftoverTime);
        ballAnimator2.setStartDelay(0);
        ballAnimator2.start();



//        // 第二个动画，bg1移动屏幕宽度的同时（移动时，bg1就移出屏幕了），
//        //      bg2移动图片宽度 x从屏幕宽度开始
//        AnimatorSet ballAnimator2 = new AnimatorSet();
//        Tools.logByWh("yyy|-leftover" + -leftover);
//        Tools.logByWh("yyy|-backgroundWidth" + backgroundWidth1);
//        Tools.logByWh("yyy|-screenWidth" + screenWidth);
//        ValueAnimator ballAnimator2X = ValueAnimator.ofFloat(-leftover, -backgroundWidth1);
//        ballAnimator2X.addUpdateListener(new ValueAnimator.AnimatorUpdateListener() {
//            @Override
//            public void onAnimationUpdate(ValueAnimator animation) {
//                Tools.logByWh("yyy-" + (float) animation.getAnimatedValue());
//                image_bg_1.setX((float) animation.getAnimatedValue());
//                image_bg_1.requestLayout();
//            }
//        });
//        ballAnimator2X.setDuration(screenTime);
//        //      bg2移动图片宽度 x从屏幕宽度开始
//        AnimatorSet ballAnimator3 = new AnimatorSet();
//        ValueAnimator ballAnimator3X = ValueAnimator.ofFloat(screenWidth, -leftover);
//        ballAnimator3X.addUpdateListener(new ValueAnimator.AnimatorUpdateListener() {
//            @Override
//            public void onAnimationUpdate(ValueAnimator animation) {
//                Tools.logByWh("xxx-" + (float) animation.getAnimatedValue());
//                image_bg_2.setX((float) animation.getAnimatedValue());
//                image_bg_2.requestLayout();
//            }
//        });
//        ballAnimator3X.setDuration(backgroundTime);
////        ballAnimator3X.setDuration(leftoverTime);
//        //组合播放
//        ballAnimator2.play(ballAnimator2X);//.after(leftoverTime)
//        ballAnimator3.play(ballAnimator3X);//.after(leftoverTime)
////        ballAnimator2.setStartDelay(leftoverTime);
//        ballAnimator2.setStartDelay(0);
//        ballAnimator3.setStartDelay(0);
//        ballAnimator2.start();
//        ballAnimator3.start();





        // 第三个动画，bg2移动屏幕宽度的同时（移动时，bg2就移出屏幕了），
        //      bg1移动图片宽度 x从屏幕宽度开始

    }


    /**
     * 获取屏幕宽高
     */
    private static int screenWidth;
    private static int screenHeight;
    private static void initCenter(Activity activity) {
        Display display = activity.getWindowManager().getDefaultDisplay();
        screenWidth = display.getWidth();
        screenHeight = display.getHeight();
//        width = 1080;
//        height = 2160;
        Tools.logByWh("屏幕 - width = " + screenWidth + " height = " + screenHeight);
    }

}
